package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.dto.PageDTO;
import com.leyou.common.exception.LyException;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.entity.CategoryBrand;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.item.service.BrandService;
import com.leyou.item.service.CategoryBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;
import java.util.stream.Collectors;

@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {
    @Autowired
    private CategoryBrandService categoryBrandService;
    @Autowired
    private BrandService brandService;

    /**
     * 根据品牌id集合查询品牌集合
     */
    @Override
    public List<BrandDTO> listBrandByIds(List<Long> ids) {

            return BrandDTO.convertEntityList(this.listByIds(ids));
    }


    /**
     * 根据分类id查询品牌
     */
    @Override
    public List<BrandDTO> queryBrandByCategoryId(Long cid) {
        List<Long> brand = categoryBrandService
                .query()
                .eq("category_id", cid).list()
                .stream()
                .map(CategoryBrand::getBrandId)
                .collect(Collectors.toList());
        return listBrandByIds(brand);
    }

    @Override
    public BrandDTO queryBrandById(Long bid) {
        Brand id = getById(bid);
        return new BrandDTO(id);
    }

    @Override
    public PageDTO<BrandDTO> queryPage(Integer page, Integer rows) {
        //定义分页条件
        IPage<Brand> iPage = new Page<>(page, rows);
        //进行分页查询
        page(iPage);

        return new PageDTO<>(iPage.getTotal(),iPage.getPages(),BrandDTO.convertEntityList(iPage.getRecords()) );
    }

    /**
     * 新增品牌
     * @param brandDTO
     */
    @Transactional
    @Override
    public void addBrand(BrandDTO brandDTO) {
        Brand brand = brandDTO.toEntity(Brand.class);
        List<CategoryBrand> collect = brandDTO.getCategoryIds().stream().map(id -> CategoryBrand.of(id, brand.getId())).collect(Collectors.toList());

        categoryBrandService.saveBatch(collect);
    }


    /**
     * 更新品牌
     * @param brandDTO
     */
    @Transactional
    @Override
    public void updateBrand(BrandDTO brandDTO) {

        Brand brand = brandDTO.toEntity(Brand.class);
        //增加品牌
        boolean success = updateById(brand);
        if (!success){
            throw new LyException(500,"更新品牌失败");
        }

         success = categoryBrandService.remove(new QueryWrapper<CategoryBrand>().eq("brand_id", brandDTO.getId()));

        if (!success){
            throw new LyException(500,"删除中间表失败");
        }

// 3.重新插入中间表数据
        List<CategoryBrand> list = brandDTO.getCategoryIds().stream()
                .map(id -> CategoryBrand.of(id, brandDTO.getId()))
                .collect(Collectors.toList());
        // 批量写入中间表数据
        categoryBrandService.saveBatch(list);
    }

    /**
     * 根据id删除品牌
     * @param id
     */
    @Transactional
    @Override
    public void deleteBrandById(Long id) {
        brandService.removeById(id);//删除品牌表
        categoryBrandService.remove(new QueryWrapper<CategoryBrand>().eq("brand_id", id));
    }

}
